package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.AdminArea;
import com.wyh.mapper.AdminAreaMapper;
import com.wyh.request.AdminAreaRequest;
import com.wyh.service.IAdminAreaService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 地区管理业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminAreaServiceImpl implements IAdminAreaService {

    private final AdminAreaMapper baseMapper;


    @Override
    public AdminArea add(AdminAreaRequest param) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminAreaRequest request) {
        AdminArea entity = AdminArea.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminArea getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminArea> pageList(AdminAreaRequest request) {
        Page<AdminArea> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminArea> query = this.buildWrapper(request);
        IPage<AdminArea> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminArea> list(AdminAreaRequest request) {
        LambdaQueryWrapper<AdminArea> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminArea> buildWrapper(AdminAreaRequest param) {
        LambdaQueryWrapper<AdminArea> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getName())){
               query.like(AdminArea::getName ,param.getName());
        }
        if (param.getParentId() != null){
               query.eq(AdminArea::getParentId ,param.getParentId());
        }
        if (param.getLevel() != null){
               query.eq(AdminArea::getLevel ,param.getLevel());
        }
         if (StringUtils.isNotBlank(param.getCode())){
               query.like(AdminArea::getCode ,param.getCode());
        }
        if (param.getIsDelete() != null){
               query.eq(AdminArea::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(AdminArea::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(AdminArea::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
